package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.ArtistClassDetailAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.entity.ArtistClassDetailEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.view.ReplaceFragment;

import java.util.List;

/**
 * Created by dllo on 15/10/26.
 */
public class ArtistClassDetailFragment extends BaseFragment {
    private View v;
    private LinearLayout linearLayout;
    private TextView titleText;
    private String title;
    private int area,sex;
    private StringRequest request;
    private VolleySingleton requestQueue;
    private Context context;
    private List<ArtistClassDetailEntity.ArtistEntity> list;
    private ListView listview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_artist_class_detail,null);
        init();
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getBundle();
        titleText.setText(title);
        String url = Url.ARTIST_SEX
                + sex +Url.ARTIST_AREA + area ;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ArtistClassDetailEntity artistClassDetailEntity = gson.fromJson(response,ArtistClassDetailEntity.class);
                list = artistClassDetailEntity.getArtist();
                initAdapter();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context, R.string.ifNet,Toast.LENGTH_LONG);
            }
        });

        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        requestQueue.addQueue(request);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                String artist = list.get(position).getTing_uid();
                String picUrl = list.get(position).getAvatar_big();
                bundle.putString(context.getString(R.string.artistID),artist);
                bundle.putString(context.getString(R.string.picUrl),picUrl);
                ArtistDetailFragment fragment = new ArtistDetailFragment();
                fragment.setArguments(bundle);
                new ReplaceFragment(fragment,getFragmentManager(),R.id.fl_main,true,null);

            }
        });

    }

    public void init(){
        context = getActivity();
        linearLayout = (LinearLayout) v.findViewById(R.id.back_btn);
        titleText = (TextView) v.findViewById(R.id.set_back_text);
        requestQueue = VolleySingleton.getInstance(context);
        listview = (ListView) v.findViewById(R.id.artist_class_detail_listView);
    }

    public void getBundle(){
        Bundle bundle = getArguments();
        area = bundle.getInt(context.getString(R.string.area));
        sex = bundle.getInt(context.getString(R.string.sex));
        title = bundle.getString(context.getString(R.string.titleText));
    }

    public void initAdapter(){
        ArtistClassDetailAdapter adapter = new ArtistClassDetailAdapter(context,list);
        listview.setAdapter(adapter);
    }

}
